package com.etonedu.meals.machine;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.blankj.utilcode.util.LogUtils;
import com.etonedu.meals.bean.ScreenTimeBean;
import com.etonedu.meals.common.SPConst;
import com.etonedu.meals.dao.DaoUtils;
import com.etonedu.meals.http.LogReportUtils;
import com.etonedu.meals.utils.SharedPreferencesUtil;
import com.etonedu.meals.utils.Utils;

import org.xutils.x;

import java.util.List;

import io.objectbox.Box;

import static android.content.Context.ALARM_SERVICE;

/**
 * Created by Administrator on 2017/6/23.
 */

public class OperationSrceen {

    private static final String OPENSCREEN = "com.etonedu.general.edu.machine.receiver.open_screen";
    private static final String CLOSESCREEN = "com.etonedu.general.edu.machine.receiver.close_screen";
    private static PendingIntent openSender;
    private static PendingIntent closeSender;
    private static AlarmManager manager;

    /*****************************新版开关屏*********************************/
    public static void setScreen(ScreenTimeBean onOffScreen) {

        long closeTime = onOffScreen.getEndTime();// 关屏时间
        long openTime = onOffScreen.getStartTime();// 开屏时间
        long currTime = System.currentTimeMillis();// 当前时间


        openSrceen(openTime);
        closeSrceen(closeTime);

        LogUtils.d("设置开关屏时间");

    }

    // 设置开屏时间
    public static void openSrceen(long time) {
        if (manager == null) {
            manager = (AlarmManager) x.app().getSystemService(Context.ALARM_SERVICE);
        }
        LogUtils.d("设置开屏时间");
        if (time < System.currentTimeMillis()) return;

        Intent openOrClose = new Intent(OPENSCREEN);
        openOrClose.putExtra("flag",1);
        openOrClose.putExtra("openTime",time);

        openSender = PendingIntent.getBroadcast(x.app(), 1001, openOrClose, PendingIntent.FLAG_UPDATE_CURRENT);
        setAlarm(manager, openSender, time);
    }

    // 设置关屏时间
    public static void closeSrceen(long time) {
        if (manager == null) {
            manager = (AlarmManager) x.app().getSystemService(Context.ALARM_SERVICE);
        }

        LogUtils.d("设置关屏时间");

        Intent openOrClose = new Intent(CLOSESCREEN);
        openOrClose.putExtra("flag",0);
        openOrClose.putExtra("closeTime",time);

        closeSender = PendingIntent.getBroadcast(x.app(), 1000, openOrClose, PendingIntent.FLAG_UPDATE_CURRENT);

        setAlarm(manager, closeSender, time);
    }

    public static void cancelScreen() {
        if (closeSender != null) {
            alarmCancel(closeSender);
        }
        if (openSender!= null) {
            alarmCancel(openSender);
        }

    }

    private static void setAlarm(AlarmManager manager, PendingIntent sender, Long time) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            manager.setExact(AlarmManager.RTC_WAKEUP, time, sender);
        } else {
            manager.set(AlarmManager.RTC_WAKEUP, time, sender);
        }
    }

    private static void alarmCancel(PendingIntent sender) {
        AlarmManager manager = (AlarmManager) x.app().getSystemService(ALARM_SERVICE);
        manager.cancel(sender);
    }

    // 开屏定时闹钟
    public static class OpenScreen extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
            String action = intent.getAction();
            long time = intent.getLongExtra("openTime",0);
            LogUtils.d("定时开屏");
            if (OPENSCREEN.equals(action)) {
                int sdkVersion = Utils.getAndroidSDKVersion();

                if (sdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    DeviceUtils.setMachineScreen(true);

                    DeviceUtils.timerSwitchOnScreen();
                    DeviceUtils.timerSwitchOnScreenAndroid5();
                } else {
                    DeviceManager.getInstance().turnOnBackLight();
                }

                DeviceUtils.openMacineScreen();

                spu.setString(SPConst.OPERATION_SRCEEN,"1");
                LogReportUtils.logReportPublicUrl("","7","1","自动开屏");
                LogReportUtils.getPublicStateUrl("1");
            }
        }
    }

    // 关屏定时闹钟
    public static class CloseScreen extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
            String action = intent.getAction();
            long closeTime = intent.getLongExtra("closeTime",0);
            LogUtils.d("定时关屏");
            if (CLOSESCREEN.equals(action)) {
                LogUtils.d("获取下一组开关屏时间");
                int sdkVersion = Utils.getAndroidSDKVersion();

                if (sdkVersion <= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    DeviceUtils.setMachineScreen(false);

                    DeviceUtils.timerSwitchOffScreen();
                    DeviceUtils.timerSwitchOffScreenAndroid5();
                } else {
                    DeviceManager.getInstance().turnOffBackLight();
                }

                DeviceUtils.closeMacineScreen();

                spu.setString(SPConst.OPERATION_SRCEEN,"0");
                setNextScreen();
                LogReportUtils.logReportPublicUrl("","8","1","自动关屏");
                LogReportUtils.getPublicStateUrl("3");
            }
        }
    }

    // 获取下一组开关屏时间并设置
    private static void setNextScreen() {
        try {
            DaoUtils.delletScreenDaoLtCurrTime();
            Box<ScreenTimeBean> box = DaoUtils.getBoxScreenTimeBean();
            List<ScreenTimeBean> list = box.getAll();
            if (!Utils.listIsNullOrEmpty(list)) {
                ScreenTimeBean oos = list.get(0);
                setScreen(oos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
